require("./index.styl");
var _mm = require("util/mm.js");
const questionTemplate = require("./question.string");
const singleOptionsTemplate = require("./single-options.string");
const judgeOptionsTemplate = require("./judge-options.string");
const multipleOptionsTemplate = require("./multiple-options.string");
const resultTemplate = require("./result.string");
const _config = require("service/config-service.js");
const _question = require("service/question-service.js");

var page = {
	backUrl: null,
	user: null,
	dept: null,
	question: null,
	init: function () {
		this.onLoad();
		this.bindEvent();
	},
	onLoad: function () {
		const _this = this;
		_config.getBackUrl(function (res) {
			_this.backUrl = res;
		}, function (errMsg) {

		});
		//%e9%bb%84%e6%9d%be%e7%a5%a5   
		//黄松祥  三个字的url编码
		// %e6%88%b4%e5%86%ac%e7%94%9f  戴冬生
		// 保卫科   %e4%bf%9d%e5%8d%ab%e7%a7%91%20%20%20%20%20%20%20  有很多空格
		// http://localhost:8088/exam/view/index.html?user=%e6%88%b4%e5%86%ac%e7%94%9f&dept=%e4%bf%9d%e5%8d%ab%e7%a7%91%20%20%20%20%20%20%20
		_this.user = $.trim(_mm.getUrlParam("user"));
		_this.dept = $.trim(_mm.getUrlParam("dept"));
		_this.renderQuestion();
	},
	bindEvent: function () {
		let _this = this;
		let selection = null;

		//回答完毕，提交按钮
		$(document).on("click", ".submit-paper-btn", function () {
			if (_this.question.type == 0 || _this.question.type == 1) {
				//判断题或者单选题
				selection = $(".option :radio:checked").val();
			} else {
				//多选题
				let selectionArr = [];
				$(".option :checkbox:checked").each(function () {
					selectionArr.push($(this).val());
				});
				selection = selectionArr.join(",");
			}
			_question.checkAnswer({
				selection
			}, function (res) {
				_this.renderResult(res);
			}, function (errMsg) {
				_mm.errorTips(errMsg);
			});
		});
		//继续答题按钮
		$(document).on("click", ".continue-btn", function () {
			_this.renderQuestion();
		});
		//回答错误后继续答题
		$(document).on("click", '.answer-wrong-continue-btn', function () {
			_this.renderQuestion();
		});

		//返回主页按钮
		$(document).on("click", ".return-btn", function () {
			window.location.href = _this.backUrl;
		});

	},
	renderQuestion: function () {
		const _this = this;
		_question.getQuestion({
				user: _this.user,
				dept: _this.dept
			},
			function (res) {
				_this.question = res;
				const template = _mm.renderHtml(questionTemplate, {
					questionContent: res.questionContent,
					typeMsg: res.typeMsg
				});
				$(".paper-wraper").html(template);
				_this.renderOptions(res);
			},
			function (errMsg) {
				_mm.errorTips(errMsg);
			});

	},
	renderOptions: function (data) {

		let template = null;
		if (data.type == 0) { //判断题
			template = _mm.renderHtml(judgeOptionsTemplate, data);
		} else if (data.type == 1) { //单选题
			template = _mm.renderHtml(singleOptionsTemplate, data);
		} else if (data.type == 2) { //多选题
			template = _mm.renderHtml(multipleOptionsTemplate, data);
		}
		$(".options").html(template);
	},
	renderResult: function (data) {
		let rightAnswerArr = [];
		data.rightAnswerList.forEach((item) => rightAnswerArr.push(item.title));
		data.rightAnswerStr = rightAnswerArr.join(",");
		const template = _mm.renderHtml(resultTemplate, data);
		$(".result").html(template, data);
	}
};

$(function () {
	page.init();
});